package com.luxoft.scoremanager.server.dao.listimpl;

import com.luxoft.scoremanager.server.dao.ComponentDao;
import com.luxoft.scoremanager.server.model.entity.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.google.common.base.Preconditions.checkNotNull;

public class ListComponentDao implements ComponentDao {
    private static List<Component> componentList = new ArrayList<Component>();

    public ListComponentDao() {
        init();
    }

    private void init() {
        componentList.add(new Component(1, "Risk position with Extraordinary Support", "1"));
        componentList.add(new Component(2, "Growth and Changes in Exposur", "1"));
        componentList.add(new Component(3, "Risk Concentration and Risk Diversification", "1"));
        componentList.add(new Component(4, "Complexity", "1"));
        componentList.add(new Component(5, "Risks not covered by RACF", "2"));
        componentList.add(new Component(6, "Risk position with Extraordinary Support", "2"));
        componentList.add(new Component(7, "Growth and Changes in Exposur", "1"));
        componentList.add(new Component(8, "Evidence of stronger or weak Loss Experience", "1"));
    }

    @Override
    public void addComponent(Component component) {
        checkNotNull(component);
        if (component.getId() == null) {
            component.setId((int) new Date().getTime());
        }
        componentList.add(component);
    }

    @Override
    public void updateComponent(Component component) {
        checkNotNull(component);
        if (component.getId() == null) {
            component.setId((int) new Date().getTime());
            componentList.add(component);
        } else {
            componentList.remove(component);
            componentList.add(component);
        }
    }

    @Override
    public void deleteComponent(Component component) {
        checkNotNull(component);
        checkNotNull(component.getId());

        componentList.remove(component);
    }

    @Override
    public List<Component> getAll() {
        return componentList;
    }

    @Override
    public Component getById(int id) {
        checkNotNull(id);
        for (Component component : componentList) {
            if (id == component.getId()) {
                return component;
            }

        }
        return null;
    }
}
